﻿using System;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;

namespace Chisel.Core
{
    /// <summary>
    /// Helper class to make up for some of the ridiculously missing methods from the Enum class.
    /// </summary>
    public static class EnumHelper
    {
        /// <summary>
        /// Gets the values of an enumeration type and cast them to a different type.
        /// </summary>
        public static R[] GetValues<T,R>()
        {
            Type enumType = typeof(T);

            if (!enumType.IsEnum)
            {
                throw new ArgumentException("Type '" + enumType.Name + "' is not an enum");
            }

            var values = new List<R>();

            var fields = from field in enumType.GetFields()
                         where field.IsLiteral
                         select field;

            foreach (FieldInfo field in fields)
            {
                object value = field.GetValue(enumType);
                values.Add((R)value);
            }

            return values.ToArray();
        }

        /// <summary>
        /// Gets the values of an enumeration type.
        /// </summary>
        public static T[] GetValues<T>()
        {
            return GetValues<T, T>();
        }

    }

}
